<?php
namespace app\Manage\controller;
use app\common\controller\Manage;
use think\Request;
use app\common\model\TaocanOrder as TaocanOrderModel;
use app\common\model\StockList as StockListModel;
use app\common\model\User as UserModel;

/**
 * 订单模块
 * Class Order
 * @package app\seller\controller
 * @author keinx
 */
class TaocanOrder extends Manage
{
    public function index(Request $requst) 
    {
        if($requst->isAjax()) {
            $this->view->engine->layout(false);
            $condition = [];
            if($requst->request('pay_status') != '') {
                $condition[] = ['taocan.pay_status', '=', $requst->request('pay_status')];
            }
            if(!empty($requst->request('room_bn'))) {
                $condition[] = ['taocan.room_bn', 'like', '%'.$requst->request('room_bn').'%'];
            }
            if(!empty($requst->request('mobile'))) {
                $condition[] = ['user.mobile', 'like', '%'.$requst->request('mobile').'%'];
            }
            if(!empty($requst->request('taocan_name'))) {
                $condition[] = ['taocan.taocan_name', 'like', '%'.$requst->request('taocan_name').'%'];
            }
            if($requst->request('status') != '') {
                $condition[] = ['taocan.status', '=', $requst->request('status')];
            }
            $field = 'taocan.*, user.mobile as mobile, user.nickname as nickname,FROM_UNIXTIME(taocan.ctime, \'%Y-%m-%d %H:%i:%s\') as date, room.name as room_name';
            $list = \think\Db::name('taocan_order')->alias('taocan')
                    ->join('user', 'taocan.user_id = user.id')
                    ->join('room', 'taocan.room_bn = room.bn')
                    ->where($condition)
                    ->field($field)
                    ->paginate($requst->request('limit'));
            $respon['code'] = 0;
            $respon['msg'] = '';
            $respon['count'] = $list->total();
            $respon['data'] = $list->items();
            return $respon;
        }
        return $this->fetch();
    }

    //订单出单
    public function editStatus(Request $request) 
    {
        if($request->isAjax()) {
            //扣减库存操作
            $taocanOrderModel = new TaocanOrderModel();
            $taocanOrder = $taocanOrderModel->field('*')->where(['id'=>$request->post('id')])->find();
            if($taocanOrder['taocan'] && $taocanOrder['taocan'] != ""){
                $stockListModel = new StockListModel();
                $taocan = json_decode($taocanOrder['taocan'], true);
                foreach ($taocan['goods'] as &$v){
                    $stockListModel->change($taocanOrder['user_id'],$v['id'],$v['total'],1,"",$taocanOrder['room_bn']);
                }
            }
            return \think\Db::name('taocan_order')->where('id', $request->post('id'))->update(['status' => 1]);
        } else {
            response(['msg' => '请求异常'], 400);
        }
    }

    //取消订单
    public function editPayStatus(Request $request) 
    {
        $res = $this->validate(['__token__' => $request->post('token')], ['__token__'  =>  'require|token']);
        if($res !== true) { 
            return json([
                'status' => false,
                'data' => '',
                'msg' => $res,
            ], 400); 
        }
        if($request->isAjax()) {      
            $taocanOrderModel = new TaocanOrderModel();
            $taocan = $taocanOrderModel->get($request->post('id'));
            if($taocan['pay_status'] == '2'){
                return [
                    'status' => false,
                    'data' => '',
                    'msg' => '订单已取消',
                ];
            }
            $res = $taocanOrderModel->taocanCancel($request->post('id'));
            return $res;
        } else {
            return json([
                'status' => false,
                'data' => '',
                'msg' => '取消异常',
            ], 400);
        }
    }

    public function getDetail(){
        $result = [
            'status' => true,
            'msg' => '获取成功',
            'data'=> []
        ];
        $taocanOrderModel = new TaocanOrderModel();
        $booking = $taocanOrderModel->field('*')->where(['id'=>input('param.id/d')])->find();
        $userModel = new UserModel();
        $user = $userModel->field('*')->where(['id' => $booking['user_id']])->find();
        $booking['nickname'] = $user['nickname'];
        $booking['mobile'] = $user['mobile'];
        $result['data'] = $booking;
        return $result;
    }
}